<?php
function check_admin()
{
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $uid = $_SESSION['uid'];
    $sql = "select admin from user where uid = {$uid}";
    $result = $conn->query($sql)->fetch_array()['admin'];
    return $result == 'Y';
}
function delete_user($uid)
{
    require_once('db_fns.php');
    $conn = db_connect();
    $sql = "delete from user where uid={$uid}";
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('删除成功！');location.href='admin.php';</script>";
    } else {
        echo "<script>alert('删除失败！');location.href='admin.php';</script>";
    }
}
function increase_priority($mid)
{
    require_once('db_fns.php');
    $conn = db_connect();

    $sql = "update merchant set priority = priority + 1 where mid = {$mid} and priority < 3";
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('调整成功！');location.href='admin.php';</script>";
    } else {
        echo "<script>alert('调整失败！');location.href='admin.php';</script>";
    }
}
function decrease_priority($mid)
{
    require_once('db_fns.php');
    $conn = db_connect();
    $sql = "update merchant set priority = priority - 1 where mid = {$mid} and priority > 0";
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('调整成功！');location.href='admin.php';</script>";
    } else {
        echo "<script>alert('调整失败！');location.href='admin.php';</script>";
    }
}
function change_frozen($mid)
{
    require_once('db_fns.php');
    $conn = db_connect();
    $sql = "select frozen from merchant where mid = {$mid}";
    $result = $conn->query($sql)->fetch_array()['frozen'];
    $now = $result == 'Y' ? 'N' : 'Y';
    $sql = "update merchant set frozen = '{$now}' where mid = {$mid}";
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('调整成功！');location.href='admin.php';</script>";
    } else {
        echo "<script>alert('调整失败！');location.href='admin.php';</script>";
    }
}
function display_if_is_admin()
{
    if (check_admin()) {
        echo '<a href="admin.php">管理页面</a>';
    }
}
